home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d13 / pcrsep89.arc / LIFE.C < prev    next >
C/C++ Source or Header  |  1990-03-21  |  2KB  |  58 lines

  1. /* Sample C program that John Conway's Game of Life.
  2.  * Use either Small or Medium memory model
  3.  *
  4.  * Save as:            Life.C
  5.  * Compile Quick C:  QCL -W3 LIFE.C CA.OBJ
  6.  *         MSC:      CL  -W3 LIFE.C CA.OBJ
  7.  *         Turbo C:  TCC -w  LIFE.C CA.OBJ
  8.  */
  9.  
  10. #include <stdlib.h>
  11. #include <conio.h>
  12. #include <stdio.h>
  13. #include "ca2.h"
  14.  
  15. #define LIVE 64            /* Display attributes for             */
  16. #define DEAD 0                /* "living" & "dead" cells            */
  17.  
  18. void main(void)
  19. {
  20.     cainit(4,ca_array);    /* Use 80x25 CGA text mode         */
  21.  
  22.     caset(12,40,LIVE);    /* Starting universe -- one of     */
  23.     caset(12,41,LIVE);    /* many, many possible choices.    */
  24.     caset(13,39,LIVE);
  25.     caset(13,40,LIVE);
  26.     caset(14,40,LIVE);
  27.     cashow();                /* Display the starting world        */
  28.  
  29.     while(!kbhit())        /* Until the user presses a key    */
  30.         cagen();                /*  create & display next generation */
  31.  
  32.     getch();                    /* Remove waiting keystroke        */
  33.     careset();                /* Tidy up                                */
  34.     exit(0);                    /* Return to DOS                        */
  35. }
  36.  
  37. int far pascal cacell()    /* Here's where we do the work    */
  38. {
  39.     int i, count=0, result;
  40.  
  41.     for(i = NorthWest; i <= SouthEast; i++)    /* Count the neighbors    */
  42.         count += (ca_array[i] == LIVE);
  43.  
  44.     count -= (ca_array[Self] == LIVE);            /* but not yourself        */
  45.  
  46.     if (count == 2)                                    /* If 2 neighbors            */
  47.         result = ca_array[Self];                    /*    don't change        */
  48.     else if (count == 3)                                /* If 3 neighbors            */
  49.         result = LIVE;                                    /*    come alive            */
  50.     else                                                    /* Otherwise die of         */
  51.         result = DEAD;                                    /*   loneliness or crowding */
  52.     return result;
  53. }
  54.  
  55.  
  56.  
  57.  
  58.